using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using DMS.BEs;
using DMS.DAOs.NH.ConfOrm;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;

namespace DMS.NH.Testing
{
	[TestFixture]
	public class TestDatabase : ChinookTestBase
	{
		[Test]
		public  void ValidatingChinook()
		{
			Configuration cfg = GetConfiguration();
			SchemaValidator sv = new SchemaValidator(cfg);
			sv.Validate();

			SchemaUpdate su = new SchemaUpdate(cfg);
			su.Execute(true, false);
		}

		[Test]
		public void Mappings()
		{
			var document = Serialize(Config.GetMapping());
			Console.Write(document);
            
		}

		private static string Serialize(HbmMapping hbmElement)
		{
			var setting = new XmlWriterSettings {Indent = true};
			var serializer = new XmlSerializer(typeof (HbmMapping));
			using (MemoryStream memStream = new MemoryStream(2048))
			{
				using (XmlWriter xmlWriter = XmlWriter.Create(memStream, setting))
				{
					if (xmlWriter != null)
					{
						serializer.Serialize(xmlWriter, hbmElement);
						memStream.Flush();
						memStream.Position = 0;
						using (var sr = new StreamReader(memStream))
						{
							return sr.ReadToEnd();
						}
					}
				}
			}
			return string.Empty;
		}
	}
}